/**
 *  axios  请求公共类
 *  @author
 */
import axios from 'axios'
// import  '../../../static/jquery-2.1.1.min.js' http://172.25.4.55:8080

// 域名
const HOST = process.env.NODE_ENV === 'development' ? 'http://www.doyiter.com:8787' : 'https://castingyou.cn'
export function xmlHttpRequestGet (url) {
  var xhr = new XMLHttpRequest()
  xhr.open('GET', HOST + url)
  // xhr.setRequestHeader('ContentType', 'application/x-www-form-urlencoded')
  // xhr.setRequestHeader('contentType', 'application/x-www-form-urlencoded')
  xhr.onload = function () {
    // console.log(JSON.parse(xhr.responseText).infos)
    return JSON.parse(xhr.responseText).infos
  }
  xhr.send()
  // return new Promise((resolve, reject) => {
  //   var xhr = new XMLHttpRequest()
  //   xhr.open('GET', HOST + url)
  //   xhr.setRequestHeader('contentType', 'application/x-www-from-urlencoded')
  //   xhr.onload = function () {
  //     return JSON.parse(xhr.responseText)
  //   }.then(rsp => {
  //     console.log(rsp)
  //     resolve(rsp.data)
  //   })
  //   xhr.send()
  // })
}

/**
 *  get类 axios请求
 * @param url
 * @returns {Promise}
 */
export function axiosGet (url) {
  return new Promise((resolve, reject) => {
    console.log(HOST + 1)
    var url2 = HOST + url
    axios({
      url: url2,
      method: 'GET',
      headers: {
        // 'Access-Control-Allow-Credentials': 'true',
        // 'Access-Control-Allow-Headers': 'X-Requested-With,Content-Type',
        // 'Access-Control-Allow-Methods': 'GET',
        // 'Access-Control-Allow-Origin': '*'
      }
      // `headers`选项是需要被发送的自定义请求头信息
      // headers: {}
    }).then(response => {
      console.log(response)
      resolve(response.data)
    })
  })
}

/**
 *  post axios 请求
 * @param url
 * @returns {Promise}
 *  {
        // 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
        // 'Access-Control-Allow-Credentials': 'true',
        // 'Access-Control-Allow-Headers': 'X-Requested-With,Content-Type',
        // 'Access-Control-Allow-Methods': 'POST',
        // 'Access-Control-Allow-Origin': '*'
      }
 */
export function axiosPost (url, data, headers) {
  return new Promise((resolve, reject) => {
    console.dir(HOST + url)
    console.dir(data)
    console.dir(headers)
    axios({
      url: HOST + url,
      method: 'POST',
      data: data,
      headers: ('CASTING-TOKEN', 'd7dae312bc204b5c9f85e4a2613d9fad')
    }).then(response => {
      console.log('post end' + response.data)
      resolve(response.data)
    }).catch(reject => {
      console.log(url + ' POST error ')
    })
  })
}

/**
 * 分页类 get请求
 * @param url
 * @param num  页码 （1）
 * @param size  每页size（10）
 * @returns {*}
 */
export function axiosGetPage (url, num = 1, size = 10) {
  return axiosGet(`${url}?pageNum=${num}&pageSize=${size}`)
}
